package com.dandelion.business.controller;


import cn.hutool.json.JSONUtil;
import com.dandelion.business.entity.SysRole;
import com.dandelion.business.entity.vo.role.req.BindMenuReqVO;
import com.dandelion.business.entity.vo.role.req.RoleReqVO;
import com.dandelion.business.entity.vo.user.CurrentUser;
import com.dandelion.business.entity.vo.validationgroup.BaseInsert;
import com.dandelion.business.entity.vo.validationgroup.BaseUpdate;
import com.dandelion.business.service.ISysRoleMenuService;
import com.dandelion.business.service.ISysRoleService;
import com.dandelion.framework.annotations.CUAnnotation;
import com.dandelion.framework.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@Api(tags = "角色模块")
@Slf4j
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysRoleMenuService roleMenuService;

    /**
     * 菜单列表 不用分页
     *
     * @param currentUser 铜
     * @return {@link R}
     */
    @ApiOperation("角色列表")
    @PostMapping("/list")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<List<SysRole>> menuList(@ApiIgnore @CUAnnotation CurrentUser currentUser) {
        log.info("我的菜单,用户id:{}", currentUser.getUserId());
        return roleService.roleList();
    }


    @ApiOperation("新增角色")
    @PostMapping("/add")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<Boolean> addMenu(@ApiIgnore @CUAnnotation CurrentUser currentUser,
                              @RequestBody @Validated(value = {BaseInsert.class}) RoleReqVO req
    ) {
        log.info("新增角色,用户信息:{},角色信息:{}", currentUser.getUserId(), JSONUtil.toJsonStr(req));
        return roleService.add(currentUser, req);
    }

    @ApiOperation("获取角色")
    @GetMapping("/query")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<SysRole> queryRole(Long id) {
        log.info("获取角色,角色id:{}", id);
        return roleService.queryRole(id);
    }


    @ApiOperation("修改角色")
    @PostMapping("/edit")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<Boolean> editMenu(@ApiIgnore @CUAnnotation CurrentUser currentUser,
                               @RequestBody @Validated(value = {BaseUpdate.class}) RoleReqVO req
    ) {
        log.info("修改角色,用户信息:{},角色信息:{}", currentUser.getUserId(), JSONUtil.toJsonStr(req));
        return roleService.editMenu(currentUser, req);
    }


    @ApiOperation("绑定角色")
    @PostMapping("/bindRole")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<Boolean> bindMenu(@ApiIgnore @CUAnnotation CurrentUser currentUser, @RequestBody BindMenuReqVO req) {
        log.info("绑定角色,参数:{}", JSONUtil.toJsonStr(req));
        return roleMenuService.bindMenu(req);
    }


}
